package be.teletask.onvif.parsers;

import be.teletask.onvif.models.OnvifMediaProfile;
import be.teletask.onvif.models.OnvifProfile;
import be.teletask.onvif.responses.OnvifResponse;
import be.teletask.onvif.util.XmlToJsonUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tomas Verhelst on 04/09/2018.
 * Copyright (c) 2018 TELETASK BVBA. All rights reserved.
 */
public class GetProfileParser extends OnvifParser<OnvifProfile> {

    //Constants
    public static final String TAG = GetProfileParser.class.getSimpleName();
    private static final String KEY_PROFILE = "Profile";
    private static final String KEY_PTZ = "PTZConfiguration";
    private static final String ATTR_TOKEN = "token";
    private static final String ATTR_NAME = "Name";

    @Override
    public OnvifProfile parse(OnvifResponse response) {
        JSONObject bodyObjet = XmlToJsonUtils.xmlToJson(response.getXml());

        JSONObject profileObject = bodyObjet.getJSONObject("GetProfileResponse").getJSONObject("Profile");

        String profileToken = profileObject.getString("@token");
        String profileName = profileObject.getString("Name");
        String ptzToken = "",ptzName = "";
        if(profileObject.containsKey("PTZConfiguration")) {
            ptzToken = profileObject.getJSONObject("PTZConfiguration").getString("@token");
            ptzName = profileObject.getJSONObject("PTZConfiguration").getString("Name");
        }

        OnvifProfile profile = new OnvifProfile(profileName,profileToken,ptzName,ptzToken);
        return profile;
    }

}
